/*
 * @Author: ChaiHongJun
 * @Date: 2021-08-11 08:52:51
 * @LastEditors: ChaiHongJun
 * @LastEditTime: 2021-08-17 09:41:45
 * @version: 
 * @Description: 
 */
/**
 * 生产环境配置
 *
 *
*/
const { resolve } = require('path');
const glob = require('glob')
const PurgeCSSPlugin = require('purgecss-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
// const CompressionPlugin = require("compression-webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { merge } = require('webpack-merge');
// 引入基本配置
const baseWebpackConfig = require('./webpack.base.config.js')
const prodWebpackConfig = merge(baseWebpackConfig, {
  //这里是生产环境配置内容
  //生产环境打包加速缓存
  cache: {
    type: 'filesystem',
    cacheDirectory: resolve(__dirname, '.temp_prod_cache'),
  },
  mode: "production",
  optimization: {
    runtimeChunk: true,
    minimize: true,   //优化CSS
    minimizer: [
      new CssMinimizerPlugin({
        parallel: true,  //使用多进程并发执行，提升构建速度
      }),
    ],
    splitChunks: {
      // include all types of chunks
      chunks: 'all',
      // 重复打包问题
      cacheGroups: {
        vendors: { // node_modules里的代码
          test: /[\\/]node_modules[\\/]/,
          chunks: "all",
          // name: 'vendors', 一定不要定义固定的name
          priority: 10, // 优先级
          enforce: true
        }
      }
    },
  },
  plugins: [
    // 打包前 清理输出目录
    new CleanWebpackPlugin(),
    // 图片压缩
    new ImageMinimizerPlugin({
      minimizerOptions: {
        // Lossless optimization with custom option
        // Feel free to experiment with options for better result for you
        plugins: [
          ["gifsicle", { interlaced: true }],
          ["jpegtran", { progressive: true }],
          ["optipng", { optimizationLevel: 5 }],
        ],
      },
      filter: (source, sourcePath) => {
        if (source.byteLength < 8 * 1024) { //小于8Kb不优化
          return false;
        }
        return true;
      },
    }),
    //打包HTML页面
    new HtmlWebpackPlugin({
      title: 'webpack5',
      template: './src/template/index.ejs',
      filename: 'index.html',
      meta: {
        keywords: '关键词'
      },
      // 压缩html页面 开发环境不需要
      minify: {
        collapseWhitespace: true,
        keepClosingSlash: true,
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        useShortDoctype: true
      }
    }),

    // CSS Tree Shaking
    new PurgeCSSPlugin({
      paths: glob.sync(resolve(__dirname, 'src/**/*'), { nodir: true }),
    }),



    // brotli 压缩文件
    // new CompressionPlugin({
    //   filename: "[path][name].br",
    //   algorithm: 'brotliCompress',
    //   test: /\.(js|css|html|svg)$/,
    //   compressionOptions: {
    //     level: 11
    //   },
    //   threshold: 2 * 1024,
    //   minRatio: 0.8,
    //   deleteOriginalAssets: false
    // })
    // //提取CSS到单独的CSS文件
    // new MiniCssExtractPlugin({
    //   filename: "style/[name].[hash:5].css",
    // }),
    // new StylelintPlugin({
    //   //指定检查源文件所在目录
    //   files: ["./src/style/*.(css|less)"]
    // }),
  ]
})
// 最后通过 module.exports 导出
module.exports = prodWebpackConfig